﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public static class ControlHelper
    {
        public static T GetParentOf<T>(this DependencyObject dependencyObject)
        {
            var parent = GetParent(dependencyObject);
            while (parent != null && !(parent is T))
                parent = GetParent(parent);
            return (T)(object)parent;
        }

        private static DependencyObject GetParent(DependencyObject dependencyObject)
        {
            var parent = VisualTreeHelper.GetParent(dependencyObject);
            if (parent == null && dependencyObject is FrameworkElement)
                return ((FrameworkElement)dependencyObject).Parent;
            return parent;
        }
    }
}
